package com.spring.demo.publicutil.thread;

import sun.misc.Unsafe;

import java.text.SimpleDateFormat;
import java.util.StringJoiner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.IntStream;

/**
 * @author youjc
 * @date 2021年04月19日 9:29
 */
public class Text {
    volatile static int falg = 0;
    static Thread t1 = null, t2 = null;
    String str1="a";
    char[] str2 = {'b','c'};

    static int a=10;

   static {
       a/=5;
   }

    void updateData(String a,char[] b){
        a="aa";
        b[0]='q';
    }
    public static void main(String[] args) {
        a+=5;
        System.out.println(a);
        Text t = new Text();
        t.updateData(t.str1,t.str2);
        System.out.println(t.str1);
        System.out.println(t.str2);

        StringJoiner sj = new StringJoiner(",");
        String str1[]  = {"aaa","123","bbb","666"};
        for (String s : str1) {
            sj.add(s);
        }
        System.out.println("sj.toString() = " + sj.toString());
        System.out.println(sj.toString());
        IntStream.range(1,10).forEach(i->sj.add(i+""));
        System.out.println(sj.toString());



        String str="[{'paymet': '', 'time': '', 'amomon': None}]";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String offsetTime = sdf.format(System.currentTimeMillis());
        System.out.println("new Date( System.currentTimeMillis()).getTime() = " +offsetTime);


        char[] num = "123456789".toCharArray();
        char[] english = "*********".toCharArray();

        //method01(num,english);
        //method02(num,english);

    }

    /**
     * 使用volatile
     * @param num
     * @param english
     */
    public static void method01(char[] num, char[] english) {
/*        ExecutorService t = Executors.newFixedThreadPool()*/
        new Thread(() -> {
            for (int i = 0; i < num.length; i++) {
                while (true) {
                    if (falg == 0) {
                        System.out.println(num[i]);
                        falg = 1;
                        break;
                    }
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < english.length; i++) {
                while (true) {
                    if (falg == 1) {
                        System.out.println(english[i]);
                        falg = 0;
                        break;
                    }
                }
            }
        }).start();
    }

    /**
     * 使用LockSupport
     * @param num
     * @param english
     */
    public static void method02(char[] num, char[] english) {
        t1 = new Thread(() -> {
            for (int i = 0; i < num.length; i++) {
                System.out.println(num[i]);
                LockSupport.unpark(t2);
                LockSupport.park();
            }
        },"t1");
        t1.start();
        t2 = new Thread(() -> {
            for (int i = 0; i < english.length; i++) {
                LockSupport.park();
                System.out.println(english[i]);
                LockSupport.unpark(t1);
            }
        },"t2");
        t2.start();
    }


}
